﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PyramidShadow
{
    class Ghost
    {
        private Vector2 v_tmp;
        private Rectangle r_tmp;
        private Texture2D PlayerTexture;
        // Animation representing the ghost
        private PlayerAnimation playerAnimation;
        private Rectangle boundingBox;
        // State of the body
        private bool Active;
        private Level level;

        private const BoxType type = BoxType.Ghost;

        private const byte frameHeigth = 75;
        private const byte frameWidth = 75;
        private const byte columCount = 6;
        private const byte rowCount = 8; 
        private const byte frameDur = 30;

        public Rectangle BoundingBox
        {
            get { return boundingBox; }
        }

        // Get the width of the body
        public int Width
        {
            get { return PlayerTexture.Width; }
        }

        // Get the height of the body
        public int Height
        {
            get { return PlayerTexture.Height; }
        }
        
        public Ghost(Texture2D texture, Vector2 startPosition, Level level)
        {
            this.level = level;
            PlayerTexture = texture;
            playerAnimation = new PlayerAnimation(texture, startPosition, frameWidth, frameHeigth, columCount, frameDur);
            boundingBox = new Rectangle((int)startPosition.X, (int)startPosition.Y, frameWidth, frameHeigth);

            // Set the player to be active
            Active = true;
            r_tmp = new Rectangle();
            v_tmp = new Vector2();
        }

        public void Update(GameTime gameTime, KeyboardState currentKeyboardState)
        {

            /*
            // Get Thumbstick Controls
            ghost.Position.X += currentGamePadState.ThumbSticks.Left.X * playerMoveSpeed;
            ghost.Position.Y -= currentGamePadState.ThumbSticks.Left.Y * playerMoveSpeed;
            */

            r_tmp = boundingBox;
            // Use the Keyboard / Dpad
            if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
                r_tmp.X = boundingBox.X - level.playerMoveSpeed;
                if (!level.kollision.collide(r_tmp,type))
                    boundingBox.X -= level.playerMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Right))
            {
                r_tmp.X = boundingBox.X + level.playerMoveSpeed;
                if (!level.kollision.collide(r_tmp, type))
                    boundingBox.X += level.playerMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Up))
            {
                r_tmp.Y = boundingBox.Y - level.playerMoveSpeed;
                if (!level.kollision.collide(r_tmp, type))
                    boundingBox.Y -= level.playerMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Down))
            {
                r_tmp.Y = boundingBox.Y + level.playerMoveSpeed;
                if (!level.kollision.collide(r_tmp, type))
                    boundingBox.Y += level.playerMoveSpeed;
            }

            playerAnimation.Update(gameTime,currentKeyboardState,boundingBox);

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            playerAnimation.Draw(spriteBatch);
        }
    }
}
